package icu.decade.store.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.annotation.Nonnull;

/**
 * @author qk
 * @version 1.0
 * @className BeanContainer
 * @description
 * @date 2023/6/20 17:18
 **/
@Service
public class BeanContainer implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(@Nonnull ApplicationContext applicationContext) throws BeansException {
        BeanContainer.applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> clz) {
        return applicationContext.getBean(clz);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        return ((T) applicationContext.getBean(beanName));
    }

    public static <T> T getBean(String beanName, Class<T> clz) {
        return applicationContext.getBean(beanName, clz);
    }

    /**
     * 获取的bean需要是多例的
     *
     * @param beanName
     * @param args
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName, Object... args) {
        return ((T) applicationContext.getBean(beanName, args));
    }

    /**
     * 获取的bean需要是多例的
     *
     * @param clz
     * @param args
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clz, Object... args) {
        return applicationContext.getBean(clz, args);
    }


}
